import { MoveStatus, PointPos, PointXY, PointYBound } from "./Macro";

const { ccclass, property } = cc._decorator;
@ccclass
export default class TouchTest extends cc.Component {

    @property(cc.Node)
    private nodeArr: cc.Node[] = [];

    private trackPoints = []; // 保存轨迹点的数组
    private originPos: any = null;
    ctx: cc.Graphics = null
    collider: cc.PolygonCollider = null
    colliderPoints: any = null
    nowX: number = null
    nowY: number = null
    isDrawRound: boolean = false
    nodeIndex: number = -1  //边界点的索引
    moveStatus: number = null;
    turnFlag: boolean = true
    judgeFlag: boolean = true  //是否需要判断交点，到达交点时关闭，离开交点后打开
    lastRadians: number = Math.PI
    currentRadians: number = Math.PI  //当前弧度
    deltaRadians: number = null
    // ctx.arc(0, 0, raduis, cc.misc.degreesToRadians(startAngle), cc.misc.degreesToRadians(endAngle), false)
    start() {
        cc.director.getPhysicsManager().enabled = true;
        this.collider = this.node.getComponent(cc.PolygonCollider)
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this)
    }
    private onTouchStart(e) {
        let touchPos = e.getLocation()
        touchPos = this.node.parent.convertToNodeSpaceAR(cc.v3(touchPos.x, touchPos.y))
        this.colliderPoints = this.node.getComponent(cc.PolygonCollider).points
        let isInCollider = cc.Intersection.pointInPolygon(cc.v2(touchPos.x, touchPos.y), this.colliderPoints)
        console.log('touchxY', touchPos.x, touchPos.y);
        if (isInCollider) {
            //判断在哪个交点
            this.node.on(cc.Node.EventType.TOUCH_MOVE, this.drawFunc, this)
            this.originPos = touchPos
            this.ctx = this.node.getComponent(cc.Graphics)
            this.ctx.lineWidth = 20
            this.ctx.lineCap = cc.Graphics.LineCap.ROUND
            this.ctx.strokeColor = cc.color(110, 142, 193)
            this.nowX = this.originPos.x + this.collider.offset.x
            this.nowY = this.originPos.y + this.collider.offset.y
            // this.initLimit(touchPos)
            this.ctx.clear()
            this.ctx.moveTo(this.nowX, this.nowY)
            this.ctx.stroke()
        }
    }
    //初始化范围
    private initLimit(pos, delta = null) {
        if (pos.y > LimitMacro.CenterYMin && pos.y < LimitMacro.CenterYMax) {  //顶点0的横边上
            this.nowY = PointXY.CenterY
        }
        if (pos.x > LimitMacro.CenterXMin && pos.x < LimitMacro.CenterXMax && pos.y > LimitMacro.CenterYMin) {  //顶点0上的竖边
            this.nowX = PointXY.CenterX
        }
        if (pos.y > LimitMacro.MiddleYMin && pos.y < LimitMacro.MiddleYMax) {  //顶点1上的横边
            this.nowY = PointXY.MiddleY
        }
        if (pos.y > LimitMacro.MiddleTopYMin && pos.y < LimitMacro.MiddleTopYMax) {  //顶点2上的横边
            this.nowY = PointXY.MiddleTopY
        }
        if (pos.x > LimitMacro.LeftBottomXMin && pos.x < LimitMacro.LeftBottomXMax && pos.y > LimitMacro.CenterYMin) {  //矩形坐下顶点4上的竖边
            this.nowX = PointXY.LeftBottomX
        }
        if (pos.x > LimitMacro.RoundLeftXMin && pos.x < LimitMacro.RoundLeftXMax && pos.y > LimitMacro.CenterYMin) {  //半圆左顶点顶点5
            this.nowX = PointXY.RoundLeftX
        }
        if (pos.x > LimitMacro.RoundRightXMin && pos.x < LimitMacro.RoundRightXMax && pos.y > LimitMacro.CenterYMin) {  //半圆右顶点顶点6
            this.nowX = PointXY.RoundRightX
        }
        if (this.nowY < LimitMacro.RoundYXMin) {
            this.isDrawRound = true
        }else{
            this.isDrawRound = false
        }

    }
    private drawFunc(e) {
        console.log('nowxyt', this.nowX, this.nowY)
        //判断移动是否超出区域
        let touchPos = e.getLocation()
        touchPos = this.node.parent.convertToNodeSpaceAR(cc.v3(touchPos.x, touchPos.y))
        let delta = e.getDelta()
        let isInCollider = cc.Intersection.pointInPolygon(cc.v2(touchPos.x, touchPos.y), this.colliderPoints)
        if (!isInCollider) {
            this.node.off(cc.Node.EventType.TOUCH_MOVE, this.drawFunc, this)
            console.log('GameOver');
            this.ctx.clear()
            return;
        }
        this.initLimit(touchPos)
        if (!this.judgeFlag) {
            if (Math.abs(delta.x) > Math.abs(delta.y)) {
                this.moveStatus = MoveStatus.HOR
            } else {
                this.moveStatus = MoveStatus.VER
            }
        }
        if (((delta.x != 0 || delta.y != 0) && this.turnFlag)) {  //没到交点的时候一直保持前进状态，不需要判断
            if (Math.abs(delta.x) > Math.abs(delta.y)) {
                this.moveStatus = MoveStatus.HOR
            } else {
                this.moveStatus = MoveStatus.VER
            }
            this.turnFlag = false
        }
        let nowIndex = this.nodeIndex
        this.nodeIndex = this.judgePointPos(cc.v2(this.nowX, this.nowY))
        if (nowIndex != this.nodeIndex) {
            this.turnFlag = true
        }

        if (!this.isDrawRound) {
            if (this.moveStatus == MoveStatus.HOR) {
                this.nowX += delta.x
            } else if (this.moveStatus == MoveStatus.VER) {
                this.nowY += delta.y
            }
            this.ctx.lineTo(this.nowX, this.nowY)
        } else {  //在圆弧上描
            if (touchPos.x > PointXY.RoundRightX || touchPos.x < PointXY.RoundLeftX) {
                this.isDrawRound = false
                // this.nowX = PointXY.RoundRightX
                // this.nowY = PointXY.CenterY
                console.log('半圆画完');
            } else {
                this.currentRadians = Math.PI
                if (touchPos.x >= PointXY.CenterX) {
                    let delta = (Math.PI - Math.acos((Math.abs(touchPos.x - PointXY.CenterX) / LimitMacro.RoundR)))
                    this.currentRadians += delta
                } else {
                    this.currentRadians += Math.acos((Math.abs(touchPos.x - PointXY.CenterX) / LimitMacro.RoundR))
                }
                this.nowX = touchPos.x
                this.nowY = - Math.sin(Math.acos((Math.abs(touchPos.x - PointXY.CenterX) / LimitMacro.RoundR))) * LimitMacro.RoundR
                if(this.lastRadians > this.currentRadians){
                    console.log('反方向');
                }else{
                    this.ctx.arc(PointXY.CenterX + 5, PointXY.CenterY, LimitMacro.RoundR, this.lastRadians, this.currentRadians, true)
                }
                this.lastRadians = this.currentRadians
            }
            console.log('nowX', this.nowX);
        }
        this.trackPoints.push(cc.v2(this.nowX, this.nowY))
        this.ctx.stroke()
        // console.log(this.trackPoints);
    }
    private judgePointPos(Pos) {  //判断在哪个交点  
        for (let i = 0; i < this.nodeArr.length; i++) {
            if (Math.abs(this.nodeArr[i].x - Pos.x) < 10 && Math.abs(this.nodeArr[i].y - Pos.y) < 10) {
                this.nodeIndex = i
                if (this.judgeFlag) {
                    this.judgeFlag = false
                    console.log(`到达交点${this.nodeIndex}`);
                }
                break;
            }
            if (i == this.nodeArr.length - 1) {
                if (!this.judgeFlag) {
                    console.log('离开了交点');  //做下面逻辑
                }
                this.judgeFlag = true
            }
        }
        return this.nodeIndex
    }
    private onTouchEnd(e) {
        console.log('游戏结束');
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.drawFunc, this)
        this.ctx = this.node.getComponent(cc.Graphics)
        this.ctx.clear()
        this.initData()
    }
    private initData() {
        this.ctx = null
        this.originPos = null
        this.colliderPoints = null
        this.nowX = this.nowY = null
        this.moveStatus = null
        this.turnFlag = true
        this.judgeFlag = true
        this.isDrawRound = false
        this.lastRadians = Math.PI
        this.currentRadians = Math.PI  //当前弧度
    }
    //在圆弧上移动(通过x坐标和R半径拿到角度获取Y坐标)
    private RoundMoveFunc() {

    }
    private isPointInTrack(point) {
        return this.trackPoints.some(p => p.fuzzyEquals(point, 3))
    }
    protected onDisable(): void {
    }

}

